define(function(require, exports, module){
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	var AJAX = require("resource/js/component/ajax.js");
	var C = require("resource/js/config/const.js");
	var EUtil = require("resource/js/component/easyutil.js");
	var easycombobox = require("resource/js/component/easycombobox.js");
	combobox.create({
		url:BASEPATH+"/base/authoriza/basecode/getType.htm",
		param:{checkeds:"isCurrent"}
	})
	var dgutil = undefined;
	var config = {
		name:"沥青搅拌机设备设置表",
		column:[[
			{field:'id',title:'主键ID',width:100,align:'center',height:50,hidden:true},
		
			{field:'stationId',title:'沥青拌合站',width:100,align:'center',height:50,hidden:false},
		
			{field:'formulaName',title:'配方名称',width:100,align:'center',height:50,hidden:false},
		
			{field:'layerId',title:'结构层',width:100,align:'center',height:50,hidden:false},
		
			{field:'materialId',title:'混合料类型',width:100,align:'center',height:50,hidden:false},
		
		]]
	}
	
	var initpage = function(){
		dgutil = EUtil.ajaxDataGrid(datagridconfig);
	}
	var initFiledByForm = function(rows,id){
		var validate = true;
		if(typeof rows == "boolean"){
			validate = rows;
		}
		combobox.init({ ele:"#isCurrent_"+id, grouptype:"isCurrent"});
		//初始化拌合站
		EUtil.ajaxHandler(BASEPATH+"/dcs/asphalt/station/queryStationList.htm",null,function(code,msg,data){
			easycombobox.init({ ele:"#stationId_"+id,valueField:'id',remote:false, textField:'stationNo'}).load(data);
		});

		//初始化结构层
		EUtil.ajaxHandler(BASEPATH+'/dcs/sys/layer/querySysLayerList.htm',null,function(code,msg,data){
			if(data!=null&&data.length>0){
				easycombobox.init({ ele:"#layerId_"+id,valueField:'id', textField:'layerName',remote:true,prompt:'请选择结构层',
					onChange:function(rec){
						$("#materialId_"+id).combobox("clear");
						$("#materialId_"+id).combobox("loadData",[]);
						//初始化混合材料
						EUtil.ajaxHandler(BASEPATH+"/dcs/sys/material/queryMaterialListByLayerId.htm",{layerId:rec},function(code,msg,data){
							easycombobox.init({ ele:"#materialId_"+id,valueField:'id',remote:true, textField:'materialName'}).load(data);
						});
					},onLoadSuccess:function(){
						//$("#layerId_add").combobox("select",data[0].id);
					}
				}).load(data);
			}
		});
		EUtil.ajaxHandler(BASEPATH+"/dcs/sys/material/queryMaterialListByLayerId.htm",{layerId:$("#layerId_"+id).combobox("getValue")},function(code,msg,data){
			easycombobox.init({ele:"#materialId_"+id,valueField:'id',remote:true, textField:'materialName'}).load(data);
		});
	}


	function initLayerAndMaterial(pid, cid, row) {
		/**
		 * 一级
		 */
		A.init().sync().success(function (code, msg, data) {
			easycombobox.init({
				ele: "#" + pid,
				valueField: 'id',
				remote: false,
				textField: 'name',
				prompt: '请选择',
				onChange: function (rec) {
					$("#" + cid).combobox("clear");
					A.init().sync().success(function (code, msg, data) {
						easycombobox.init({
							ele: "#" + cid,
							valueField: 'id',
							remote: false,
							textField: 'name',
							prompt: '请选择'

						}).load(data);
					}).post(BASEPATH + '/base/authoriza/conmonData/queryDataNoCache.htm', {
						queryId: 'getDcsMaterial',
						param: $("#" + pid).combobox("getValue")
					});
				}
			}).load(data);
		}).error(function (code, msg, data) {
			$.messager.alert("系统提示", msg, "error");
		}).post(BASEPATH + '/base/authoriza/conmonData/queryDataNoCache.htm', {"queryId": "getDcsLayer"});

		/**
		 * 二级
		 */
		A.init().sync().success(function (code, msg, data) {
			easycombobox.init({
				ele: "#" + cid,
				valueField: 'id',
				remote: false,
				textField: 'name',
				prompt: '请选择',
				onChange: function (rec) {

				}
			}).load(data);
		}).post(BASEPATH + '/base/authoriza/conmonData/queryDataNoCache.htm', {
			queryId: 'baseareaparam',
			param: $("#" + pid).combobox("getValue")
		});
		if (row && row.layerId) {
			$("#" + pid).combobox("setValue", row.layerId);
		}
		if (row && row.materialId) {
			$("#" + cid).combobox("setValue", row.materialId);
		}
	}

	var getRandom = function(){
		var hostName = window.location.hostname;
		if(hostName == 'localhost'){
			hostName = '127.0.0.1';
		}
		return hostName.replace(/\./g,'')+(new Date().getTime())+Math.round(Math.random()*10000);
	}

	var addDialogInit = function(url){
		$("#editdlg").dialog({
			title:config.name+"增加",
			href:url,
			onLoad:function(){
				$("#stationId_add").val($("#stationId").val());
				initFiledByForm(false,'add');
				$('#token_id_add').val(getRandom());
		    },
			buttons: [{
				text:'保存',
				iconCls:'icon-ok',
				accessKey:{akey:18,mkey:83},
				handler:function(){
					using(["form"],function(){
						if($("#addform1").form("validate")){
							if($("#addform2").form("validate")){
								var obj = $("#addform1").serializeJson();
								var arr = ['0063','0125','025','05','1','2','4','5','63','8','10','125','14','16','20','25'];
								var arrPore = [];
								for (var i = 0; i < arr.length; i ++){
									var poreList = {};
									poreList.poreType = $('#'+arr[i]+'poreType_add').val();
									poreList.designValue = $('#'+arr[i]+'designValue_add').val();
									poreList.workMin = $('#'+arr[i]+'workMin_add').val();
									poreList.workMax = $('#'+arr[i]+'workMax_add').val();
									poreList.designMin = $('#'+arr[i]+'designMin_add').val();
									poreList.designMax = $('#'+arr[i]+'designMax_add').val();
									poreList.aggregate1 = $('#'+arr[i]+'aggregate1_add').val();
									poreList.aggregate2 = $('#'+arr[i]+'aggregate2_add').val();
									poreList.aggregate3 = $('#'+arr[i]+'aggregate3_add').val();
									poreList.aggregate4 = $('#'+arr[i]+'aggregate4_add').val();
									poreList.aggregate5 = $('#'+arr[i]+'aggregate5_add').val();
									poreList.aggregate6 = $('#'+arr[i]+'aggregate6_add').val();
									poreList.aggregate7 = $('#'+arr[i]+'aggregate7_add').val();
									poreList.aggregate8 = $('#'+arr[i]+'aggregate8_add').val();
									poreList.powder1 = $('#'+arr[i]+'powder1_add').val();
									poreList.powder2 = $('#'+arr[i]+'powder2_add').val();
									poreList.powder3 = $('#'+arr[i]+'powder3_add').val();
									poreList.powder4 = $('#'+arr[i]+'powder4_add').val();
									arrPore.push(poreList);
								}
								obj.arrPoreJson = JSON.stringify(arrPore);
								
								/*EUtil.ajaxHandler(BASEPATH + "/dcs/asphalt/device/saveMixAsphaltBlender.htm", obj, function () {
									dgutil.pager();
									$("#editdlg").dialog("close");
								});*/
								$.ajax({
									type: 'POST',
									url: BASEPATH+"/dcs/asphalt/device/saveMixAsphaltBlender.htm",
									data: obj,
									dataType: 'json',
									success: function (data) {
										dgutil.pager();
										if(data.msg.length > 0){
											$.messager.show({title:C.MESSAGE.MSGTITLE,msg:data.msg});
										}
										$("#editdlg").dialog("close");
									}
								});
								
							}
						}
					});
				}
			},{
				text:'取消',
				addClass:'l-btn-red',iconCls:'icon-not',
				accessKey:{akey:18,mkey:67},
				handler:function(){
					$('#editdlg').dialog('close');
				}
			}]
		});
		$("#editdlg").dialog("open");
	}
	var updDialogInit = function(url,row){
		$("#editdlg").dialog({
			iconCls: 'icon-save',
			title:config.name+'修改',
			href:url,
			onLoad:function(){
				var poreList = row.poreList;
				$("#blendId_upd").val(row.blenderId);
				initFiledByForm(row,'upd');
				$("#updform1").form("load",row);
				for(var i in poreList){
					$("#poreId_upd").val(poreList[i].poreId);
					$("#updform2").form("load",poreList[i]);
				}
				$('#token_id_upd').val(getRandom());
		    },
			buttons: [{
				text:'保存',
				iconCls:'icon-ok',
				accessKey:{akey:18,mkey:83},
				handler:function(){
					using(["form"],function(){
						if($("#updform1").form("validate")){
							if($("#updform2").form("validate")){
								var obj = $("#updform1").serializeJson();
								var arr = ['0063','0125','025','05','1','2','4','5','63','8','10','125','14','16','20','25'];
								var arrPore = [];
								for (var i = 0; i < arr.length; i ++){
									var poreList = {};
									poreList.poreType = $('#'+arr[i]+'poreType_upd').val();
									poreList.designValue = $('#'+arr[i]+'designValue_upd').val();
									poreList.workMin = $('#'+arr[i]+'workMin_upd').val();
									poreList.workMax = $('#'+arr[i]+'workMax_upd').val();
									poreList.designMin = $('#'+arr[i]+'designMin_upd').val();
									poreList.designMax = $('#'+arr[i]+'designMax_upd').val();
									poreList.aggregate1 = $('#'+arr[i]+'aggregate1_upd').val();
									poreList.aggregate2 = $('#'+arr[i]+'aggregate2_upd').val();
									poreList.aggregate3 = $('#'+arr[i]+'aggregate3_upd').val();
									poreList.aggregate4 = $('#'+arr[i]+'aggregate4_upd').val();
									poreList.aggregate5 = $('#'+arr[i]+'aggregate5_upd').val();
									poreList.aggregate6 = $('#'+arr[i]+'aggregate6_upd').val();
									poreList.aggregate7 = $('#'+arr[i]+'aggregate7_upd').val();
									poreList.aggregate8 = $('#'+arr[i]+'aggregate8_upd').val();
									poreList.powder1 = $('#'+arr[i]+'powder1_upd').val();
									poreList.powder2 = $('#'+arr[i]+'powder2_upd').val();
									poreList.powder3 = $('#'+arr[i]+'powder3_upd').val();
									poreList.powder4 = $('#'+arr[i]+'powder4_upd').val();
									arrPore.push(poreList);
								}
								obj.arrPoreJson = JSON.stringify(arrPore);
								/*EUtil.ajaxHandler(BASEPATH + "/dcs/asphalt/device/updateMixAsphaltBlender.htm", obj, function () {
									dgutil.pager();
									$("#editdlg").dialog("close");
								});*/
								$.ajax({
									type: 'POST',
									url: BASEPATH+"/dcs/asphalt/device/updateMixAsphaltBlender.htm",
									data: obj,
									dataType: 'json',
									success: function (data) {
										dgutil.pager();
										if(data.msg.length > 0){
											$.messager.show({title:C.MESSAGE.MSGTITLE,msg:data.msg});
										}
										$("#editdlg").dialog("close");
									}
								});
							}
						}
					});
				}
			},{
				text:'取消',
				addClass:'l-btn-red',iconCls:'icon-not',
				accessKey:{akey:18,mkey:67},
				handler:function(){
					$('#editdlg').dialog('close');
				}
			}]
		});
		$("#editdlg").dialog("open");
	}
	var delInit = function(url,rows){
		var checks = [];
		for(var key in rows){
			var id = '' +rows[key].id;
			checks.push(id);
		}
		EUtil.ajaxHandler(url,{checkeds:checks.join(",")},function(){
			dgutil.delRow(rows);
		});
	}
	module.exports = {
		trans:function(){
			var buttons ={ 
				title:{
					_class:"",
					text:"沥青搅拌机设备设置表列表"
				},
				btn:[
					 {
						 iconCls: 'icon-search',
						 url:'/dcs/asphalt/device/queryMixAsphaltBlenderByPage.htm',
						 text:"查询",
						 //addClass:'l-btn-display',
						 btnType: 'searchBtn',
						 handler: function(){dgutil.pager(1);}
					 },
					 {
						 iconCls: 'icon-add',
						// addClass:'l-btn-display',
						 text:"添加",
						 url:'/view/dcs/asphalt/device/mixasphaltblenderadd',
						 handler: addDialogInit
					 },
					 {
						 iconCls: 'icon-edit',
						 url:'/view/dcs/asphalt/device/mixasphaltblenderupd',
						 text:"修改",
						// addClass:'l-btn-display',
						 needRow:true,
						 handler: updDialogInit
					 },
					 {
						 iconCls: 'icon-remove',
						 url:'/dcs/asphalt/device/deleteMixAsphaltBlender.htm',
						 text:"删除",
						// addClass:'l-btn-display',
						 needRows:true,
						 isCheck:true,
						 handler: delInit
					 }
				]};
			datagridconfig = {
				fitColumns:true,
				singleSelect:false,
				striped:true,
				autoRowHeight:false,
				fit: true,
				toolbar:buttons,
				columns:config.column,
				eid:"dg",
				url:BASEPATH+'/dcs/asphalt/device/queryMixAsphaltBlenderByPage.htm',
				args:"searchform"
			};
			
			using(["parser","dialog","datagrid","combobox" ],function(){
				initpage();
			});
		}
	}
});
